#include "mycoin.h"
#include "dataconfig.h"
#include <QPainter>
#include <QDebug>
#include <QPixmap>
#include <QLabel>
#include "dataconfig.h"

//MyCoin::MyCoin(QWidget *parent) : QPushButton(parent)
//{

//}

MyCoin::MyCoin(QString btnIMG)
{
    QPixmap pix;
   // QLabel *label = new QLabel(this);
    bool ret = pix.load(btnIMG);
    if(!ret) qDebug()<<btnIMG<<"加载错误";

    timer1 = new QTimer(this);
    timer2 = new QTimer(this);

    this->setFixedSize(pix.width(),pix.height());
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));

    connect(timer1,&QTimer::timeout,[=](){
        //点击的币为金币

        QPixmap pix(str[this->i++]);
        this->setFixedSize(pix.width(),pix.height());

        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));

        //label->setPixmap(pix);

        if(this->i==8)
        {
            this->i=0;
//            this->flag = false;
            timer1->stop();
            this->Is_Turning = false;
        }

    });
    //点击的币为银币

    connect(timer2,&QTimer::timeout,[=](){

        QPixmap pix(str[this->j--]);

        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));

        //label->setPixmap(pix);

        if(this->j==-1)
        {
            this->j=7;
//            this->flag = true;
            timer2->stop();
            this->Is_Turning = false;
        }

    });

}

void MyCoin::changeFlag()
{
    //如果是正面 翻成反面
    if(this->flag)
    {
        timer1->start(30);
        Is_Turning=true;//开始做动画
        this->flag=false;
    }
    else
    {//反面翻正面
        timer2->start(30);
        Is_Turning=true;//开始做动画
        this->flag=true;

    }


}
void MyCoin::mousePressEvent(QMouseEvent *e)
{
    if(this->Is_Turning || this->is_Win)
    {
        return;
    }
    else
    {
        QPushButton::mousePressEvent(e);
    }

}
